package Zuoye;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class BookText {
    public static void main(String[] args) {
        ArrayList<Book> books=new ArrayList<>();
        Book book1 = new Book();
        book1.setName("活着");
        book1.setPrize(13.7);
        Book book2 = new Book();
        book2.setName("百年孤独");
        book2.setPrize(33.3);
        Book book3 = new Book();
        book3.setName("三体");
        book3.setPrize(55.4);
        Book book4 = new Book();
        book4.setName("小王子");
        book4.setPrize(33.3);
        Book book5 = new Book();
        book5.setName("吕氏春秋");
        book5.setPrize(31.12);
        books.add(book1);books.add(book2);
        books.add(book3);books.add(book4);
        books.add(book5);
        for (Book ele:books){
            System.out.println("书名： " + ele.getName());
            System.out.println("书的价格" + ele.getPrize());
        }
        System.out.println("====================");
        System.out.println("自定义排序");
      Collections.sort(books, new Comparator<Book>() {
          @Override
          public int compare(Book o1, Book o2) {
              return o1.getPrize().compareTo(o2.getPrize());
          }
      });
        System.out.println("books = " + books);
        for (int i = 0; i < books.size(); i++) {
            Book book=(Book) books.get(i);
            int index=book.getPrize().compareTo(33.3);
            if (index==0){
                System.out.println("《"+book.getName()+"》");
            }
        }
    }
}
